package com.wu.controller.system;

import com.wu.base.ApiResponse;
import com.wu.base.PageInfoData;
import com.wu.base.ResultUtils;
import com.wu.controller.BaseController;
import com.wu.enums.SystemEnum;
import com.wu.pojo.reqeust.CommonIdRequest;
import com.wu.pojo.reqeust.sys.EditPasswordRequest;
import com.wu.pojo.reqeust.sys.UserEditRequest;
import com.wu.pojo.reqeust.sys.UserListRequest;
import com.wu.pojo.reqeust.sys.UserSaveRequest;
import com.wu.pojo.response.sys.UserListResponse;
import com.wu.pojo.response.sys.UserOptionListResponse;
import com.wu.redis.RedisService;
import com.wu.service.sys.SUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@Api(tags = "S用户相关接口")
@RequestMapping(value = "/user")
public class SUserController extends BaseController {

    @Resource
    private SUserService userService;
    @Resource
    private RedisService redisService;

    @ApiOperation(value = "列表")
    @RequiresPermissions("/user/list")
    @PostMapping(value = "/list")
    public ApiResponse<PageInfoData<UserListResponse>> list(@Validated() @RequestBody UserListRequest request) {
        return userService.list(request);
    }

    @ApiOperation(value = "下拉选择列表")
    @RequiresPermissions("/user/optionList")
    @PostMapping(value = "/optionList")
    public ApiResponse<UserOptionListResponse> optionList(@Validated() @RequestBody UserListRequest request) {
        return userService.optionList(request);
    }

    @ApiOperation(value = "新增")
    @RequiresPermissions("/user/save")
    @PostMapping(value = "/save")
    public ApiResponse save(@Validated() @RequestBody UserSaveRequest request) {
        boolean hasKey = redisService.hasKey("/user/save_" + request.getPhone());
        if (hasKey) {
            return ResultUtils.setError(SystemEnum.REPEAT_OPERATION);
        }
        redisService.set("/user/save_" + request.getPhone(), System.currentTimeMillis(), 2);
        return userService.save(request);
    }

    @ApiOperation(value = "修改")
    @RequiresPermissions("/user/edit")
    @PostMapping(value = "/edit")
    public ApiResponse edit(@Validated() @RequestBody UserEditRequest request) {
        return userService.edit(request);
    }

    @ApiOperation(value = "删除")
    @RequiresPermissions("/user/delete")
    @PostMapping(value = "/delete")
    public ApiResponse delete(@Validated() @RequestBody CommonIdRequest request) {
        return userService.delete(request);
    }


    @ApiOperation(value = "单个查询")
    @RequiresPermissions("/user/getById")
    @PostMapping(value = "/getById")
    public ApiResponse<UserListResponse> getById(@Validated() @RequestBody CommonIdRequest request) {
        return userService.getById(request);
    }

    @ApiOperation(value = "设置为默认密码")
    @RequiresPermissions("/user/defaultPwd")
    @PostMapping("/defaultPwd")
    public ApiResponse defaultPwd(@Validated() @RequestBody CommonIdRequest request) {
        return userService.defaultPwd(request);
    }

    @ApiOperation(value = "修改账号密码")
    @RequiresPermissions("/user/editPwd")
    @PostMapping("/editPwd")
    public ApiResponse editPwd(@Validated() @RequestBody EditPasswordRequest request) {
        return userService.editPwd(request);
    }


}
